﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace MazeRaider
{
    class DfsAgent : BaseThread
    {
        private Robot robot;
        private SharedMemory memory;
        private MessageQueue queue;

        private MazeMap mazeMap;
        private bool stopFlag;

        public DfsAgent(Robot robot, SharedMemory memory, MessageQueue queue)
        {
            this.robot = robot;
            this.memory = memory;
            this.queue = queue;
        }

        public void Initialize()
        {
            mazeMap = new MazeMap();
            stopFlag = false;

            robot.StopFlag = false;
            robot.NorthRotation = null;
        }

        public override void Run()
        {
            Thread.Sleep(2500);

            RobotInfo info = robot.GetRobotInfo();
            mazeMap.AddStartingPoint(info.X, info.Y);

            Dfs();

            Console.WriteLine("Agent finished execution");
        }

        public override void Stop()
        {
            stopFlag = true;
            robot.StopFlag = stopFlag;
        }

        private void Dfs()
        {
            Console.WriteLine(mazeMap.ToString());

            RobotInfo info;
            
            info = robot.GetRobotInfo();
            if (!stopFlag && !mazeMap.IsVisited(info.X + 0.1, info.Y))
            {
                Console.WriteLine("Move North");
                robot.MoveNorth();
                info = robot.GetRobotInfo();
                mazeMap.AddPassage(info.X, info.Y);
                Dfs();
                Console.WriteLine("Move South");
                robot.MoveSouth();
            }

            info = robot.GetRobotInfo();
            if (!stopFlag && !mazeMap.IsVisited(info.X - 0.1, info.Y))
            {
                Console.WriteLine("Move South");
                robot.MoveSouth();
                info = robot.GetRobotInfo();
                mazeMap.AddPassage(info.X, info.Y);
                Dfs();
                Console.WriteLine("Move North");
                robot.MoveNorth();
            }

            info = robot.GetRobotInfo();
            if (!stopFlag && !mazeMap.IsVisited(info.X, info.Y + 0.1))
            {
                Console.WriteLine("Move East");
                robot.MoveEast();
                info = robot.GetRobotInfo();
                mazeMap.AddPassage(info.X, info.Y);
                Dfs();
                Console.WriteLine("Move West");
                robot.MoveWest();
            }

            info = robot.GetRobotInfo();
            if (!stopFlag && !mazeMap.IsVisited(info.X, info.Y - 1.0))
            {
                Console.WriteLine("Move West");
                robot.MoveWest();
                info = robot.GetRobotInfo();
                mazeMap.AddPassage(info.X, info.Y);
                Dfs();
                Console.WriteLine("Move East");
                robot.MoveEast();
            }
        }
    }
}
